﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Sc2;
using Jayden.Dll.Sc2.Descriptions;

namespace Jayden.Site.Sc2.Dll
{
	public class EntityDescriptionSection : Section
	{
		private Div m_EntityType;
		private Img m_EntityIcon;
		private H2 m_EntityName;
		private Li m_EntityMineral;
		private Li m_EntityVespene;
		private Li m_EntityTime;
		private Li m_EntitySupply;
		private EntityUl m_EntityInputs;
		private EntityUl m_EntityRequirements;
		private Ul m_EntityActions;
		private EntityUl m_EntityProducers;

		public Sc2PageHtml Page { get { return Root as Sc2PageHtml; } }

		public override void Load(System.Xml.XmlNode node, Jayden.Dll.Web.HtmlControlLoader loader)
		{
			base.Load(node, loader);

			m_EntityType = GetElementByServerId<Div>("entityType");
			m_EntityName = GetElementByServerId<H2>("entityName");
			m_EntityMineral = GetElementByServerId<Li>("entityMineral");
			m_EntityVespene = GetElementByServerId<Li>("entityVespene");
			m_EntityTime = GetElementByServerId<Li>("entityTime");
			m_EntitySupply = GetElementByServerId<Li>("entitySupply");
			m_EntityInputs = GetElementByServerId<EntityUl>("entityInputs");
			m_EntityRequirements = GetElementByServerId<EntityUl>("entityRequirements");
			m_EntityActions = GetElementByServerId<Ul>("entityActions");
			m_EntityIcon = GetElementByServerId<Img>("entityIcon");
			m_EntityProducers = GetElementByServerId<EntityUl>("entityProducers");
		}

		private EntityDescription m_Entity;
		private RaceDescription m_Race;
		private ActionDescription m_Action;

		public override void ProcessHtmlRequest(Jayden.Dll.Web.HttpHandlerContext context)
		{
			base.ProcessHtmlRequest(context);

			m_EntityInputs.ServerRender = false;
			m_EntityRequirements.ServerRender = false;
			m_EntityActions.ServerRender = false;
			m_EntityProducers.ServerRender = false;

			m_Entity = Page.Entity;
			m_Race = Page.Race;

			if (m_Entity != null)
			{
				Cost withInputs = m_Entity.CostWithInputs;
				Cost firstBuild = m_Entity.FirstBuildCost;

				m_EntityType.AddText(m_Entity.Type.ToString() + " (Level " + m_Entity.Level + ")");
				m_EntityName.AddText(m_Entity.Name);
				m_EntityIcon.Alt = m_Entity.Id;
				m_EntityIcon.Src = "/Images/" + m_Entity.IconId + ".gif";
				m_EntityIcon.Title = m_Entity.Name;
				m_EntityMineral.AddImg("Mineral", "/Images/Minerals.gif").Title = "Mineral";
				m_EntityVespene.AddImg("Vespene", "/Images/Vespene-" + m_Race.Id + ".gif").Title = "Vespene";
				m_EntityTime.AddImg("Time", "/Images/Time-" + m_Race.Id + ".gif").Title = "Time";
				m_EntitySupply.AddImg("Supply", "/Images/Supply-" + m_Race.Id + ".gif").Title = "Supply";
				m_EntityMineral.AddText<Span>(m_Entity.Mineral + " (" + withInputs.Mineral + ") [" + firstBuild.Mineral + "]");
				m_EntityVespene.AddText<Span>(m_Entity.Vespene + " (" + withInputs.Vespene + ") [" + firstBuild.Vespene + "]");
				m_EntityTime.AddText<Span>(m_Entity.Time + " (" + withInputs.Time + ") [" + firstBuild.Time + "]");
				m_EntitySupply.AddText<Span>(m_Entity.Supply + " (" + withInputs.Supply + ") [" + firstBuild.Supply + "]");

				m_EntitySupply.ServerRender = (m_Entity.Supply != 0 || withInputs.Supply != 0 || firstBuild.Supply != 0);
				m_EntityVespene.ServerRender = (m_Entity.Vespene != 0 || withInputs.Vespene != 0 || firstBuild.Vespene != 0);
				m_EntityTime.ServerRender = (m_Entity.Time != 0 || withInputs.Time != 0 || firstBuild.Time != 0);
				m_EntityMineral.ServerRender = (m_Entity.Mineral != 0 || withInputs.Mineral != 0 || firstBuild.Mineral != 0);

				m_EntityMineral.Parent.ServerRender = (m_EntitySupply.ServerRender || m_EntityVespene.ServerRender || m_EntityTime.ServerRender || m_EntityMineral.ServerRender);

				m_EntityRequirements.Set(m_Entity.Requirements);
				m_EntityProducers.Set(m_Entity.Producers);
				m_EntityInputs.Set(m_Entity.Inputs);

				foreach (ActionDescription action in m_Entity.Actions)
				{
					m_EntityActions.ServerRender = true;
					Li li = m_EntityActions.AddLi();
					A a = li.AddA(null, "?actionId=" + action.Id);
					if (action is BuildActionDescription)
						a.HRef += "&entityId=" + ((BuildActionDescription)(action)).Build.Id;
					a.AddImg(action.Id, "/Images/" + action.IconId + ".gif").Title = action.Name;
				}
			}
		}
	}
}
